package com.darwin.darwinUI.controls
{
	
	import com.darwin.darwinBitmapEngine.core.IContainer;
	import com.darwin.darwinBitmapEngine.core.INode;
	import com.darwin.darwinBitmapEngine.utils.G_ID;
	import com.darwin.darwinNameSpace.darwin;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.DarwinFont;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * 文字显示标签,继承UIComponent并实现INode接口，因此可同时在UI层和场景层使用
	 * @author Roland
	 */
	public class Label extends UIComponent implements INode
	{
		private var _tfRight:int = 10;//右边距
		
		private var _tfBottom:int = 10;//底部边距
		
		private var _textField:TextField;		
		private var _textFieldContainer:Sprite;
		private var _textFieldBitmap:Bitmap;
		private var _changed:Boolean = false;

		public function Label(text:String = "", font:DarwinFont = null,textTop:Number = 0)
		{
			super();
						
			_textFieldBitmap = new Bitmap();
			_textField = new TextField();
			_textField.selectable = false;
			_textField.mouseEnabled = false;
			_textField.mouseWheelEnabled = false;
			_textField.multiline = true;
			if (font) 
			{
				font.apply(_textField);
				_textField.textColor = font.color;
			}
//			_textField.y = textTop;

			_textFieldContainer = new Sprite();
			_textFieldContainer.addChild(_textField);
			
			this.text = text;
			_textFieldBitmap.y = textTop;
			addChild(_textFieldBitmap);
		}
		
		override protected function refresh():void
		{
			if(_changed==false)return;
			var rect:Rectangle = _textField.getBounds(_textFieldContainer);
			var bitmapData:BitmapData = new BitmapData(rect.width + _tfRight,rect.height + _tfBottom,true,0);
//			var filters:Array;
//			_textField.textColor = _textColor;
		/*	if(_textColor != 0){
				filters = _textField.filters;
				_textField.filters = [];
			}*/
			bitmapData.draw(_textField);
		/*	//变换颜色
			if(_textColor != 0)
			{
				var rf:uint = (_textColor >> 16) & 0xff;
				var gf:uint = (_textColor >> 8) & 0xff;
				var bf:uint = (_textColor) & 0xff;
				var colorTransform:ColorTransform = new ColorTransform(0,0,0,1,rf,gf,bf,0);
				bitmapData.colorTransform(rect,colorTransform);
				for (var i:int = 0; i < filters.length; i++) 
				{
					bitmapData.applyFilter(bitmapData,bitmapData.rect,new Point(),filters[i]);
				}
				_textField.filters = filters;
			}*/
			_textFieldBitmap.bitmapData = bitmapData; 
			_changed = false;
//			super.refresh();			
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			_textField.width = value;
			changed = true;
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			_textField.height = value;
			changed = true;
		}
		
		/**
		 * 显示的文本
		 * @return 
		 */
		public function get text():String
		{
			return _textField.text;
		}
		
		public function set text(value:String):void
		{
			_textField.text = value;
			if(widthAuto){
				this.width = _textField.textWidth + _tfRight;
			}
			if(heightAuto) {
				this.height = _textField.textHeight + _tfBottom;
			}
			changed = true;
		}
		

		/**
		 *	 包含文本字段内容的 HTML 表示形式
		 */
		public function get htmlText():String
		{
			return _textField.htmlText;
		}

		/**
		 * @private
		 */
		public function set htmlText(value:String):void
		{
			_textField.htmlText = value;
			if(widthAuto){
				this.width = _textField.textWidth + _tfRight;
			}
			if(heightAuto) {
				this.height = _textField.textHeight + _tfBottom;
			}
			
			changed = true;
		}

		
		public function get textWidth():Number
		{
			return _textField.textWidth;
		}
		
		public function get textHeight():Number
		{
			return _textField.textHeight;
		}
		
		/**
		 * label的位图实际宽度 
		 * @return 
		 * 
		 */
		public function get bitmapWidth():Number
		{
			return bitmapData ? bitmapData.width : 0;
		}
		
		/**
		 * label的位图实际高度  
		 * @return 
		 * 
		 */
		public function get bitmapHeight():Number
		{
			return bitmapData ? bitmapData.height : 0;
		}
		
		private var _widthAuto:Boolean = true;
		private var _heightAuto:Boolean = true;
		
		/**
		 * 宽度自适应
		 * @return 
		 */
		public function get widthAuto():Boolean
		{
			return _widthAuto;
		}
		
		public function set widthAuto(value:Boolean):void
		{
			if(_widthAuto != value)
			{
				if(value==true) {
					_textField.width = _textField.textWidth + _tfRight;
					changed = true;
				}
				_widthAuto = value;
			}
		}
		
		/**
		 * 高度自适应
		 * @return 
		 */
		public function get heightAuto():Boolean
		{
			return _heightAuto;
		}
		
		public function set heightAuto(value:Boolean):void
		{
			if(_heightAuto != value)
			{
				if(value) {
					_textField.width = _textField.textHeight + _tfBottom;
					changed = true;
				}
				_heightAuto = value;
			}
		}
		
//		private var _textColor:uint = 0;
		/**
		 * 文本颜色
		 */
		public function get textColor():uint	{
			return _textField.textColor;
		}
		public function set textColor(value:uint):void	{
			_textField.textColor = value;
			changed = true;
		}
		
		/**
		 * 文本透明度
		 */
		public function get textAlpha():Number	{
			return _textFieldBitmap.alpha;//_textField.alpha;
		}
		public function set textAlpha(value:Number):void 
		{
			_textFieldBitmap.alpha = value;//_textField.alpha = value;
		}
		
		public function set textFilters(value:Array):void
		{
			_textField.filters = value;
			changed = true;
		}
		
		/**
		 * 加在文本上的滤镜组
		 */
		public function get textFilters():Array
		{
			return _textField.filters;	
		}
		
		/**
		 * 一个布尔值，指示文本字段是否自动换行。如果 wordWrap 的值为 true，则该文本字段自动换行；如果值为 false，则该文本字段不自动换行。默认值为 false。 
		 * @param v
		 * 
		 */
		public function set wordWrap(v:Boolean):void
		{
			_textField.wordWrap = v;
			changed = true;
		}
		
		public function get wordWrap():Boolean
		{
			return _textField.wordWrap;
		}
		
		/**
		 *	便签是否有变化 
		 */
		protected function get changed():Boolean
		{
			return _changed;
		}
		
		/**
		 * @private
		 */
		protected function set changed(value:Boolean):void
		{
			_changed = value;
			refresh();
		}
		
		//////////////////////////////////////////////////////////////////////
		//
		//  实现INode接口部分
		//
		/////////////////////////////////////////////////////////////////////
		/** 
		 * 索引ID。生成显示列表全局唯一ID
		 */
		private var _g_ID:uint = 0;
		public function get g_ID():uint
		{
			if(_g_ID == 0)
			{
				_g_ID = G_ID.id;
			}
			return _g_ID;
		}
		
		// 
		/**
		 * 取得要显示的位图
		 * @return 
		 */
		public function get bitmapData():BitmapData
		{
			return _textFieldBitmap.bitmapData;
		}

		
		public function render():void
		{
			refresh();
		}
		
		private var _rect:Rectangle;
		/**
		 * 设置显示区域，如果不设置则返回位图区域
		 * @param value 
		 */
		public function set rect(value:Rectangle):void
		{
			_rect = value;
		}
		/**
		 * 取得位图区域，默认为位图区域
		 * @return 返回当前区域 
		 */
		public function get rect():Rectangle
		{
			if(_textFieldBitmap == null) return null;
			return _rect || bitmapData.rect;
		}
		
		private var _parent:IContainer;
		/**
		 * 所在的父级
		 * @param value
		 */
		public function set parentContainer(value:IContainer):void
		{
			_parent = value;
		}
		
		public function get parentContainer():IContainer
		{
			return _parent;
		}
		
		
		/**
		 * 是否允许单击,Label组件不接受鼠标事件，此处永远返回false
		 */
		public function set clickEnabled(value:Boolean):void
		{
			//Label不接受鼠标事件
		}
		public function get clickEnabled():Boolean
		{
			return false;
		}
		
		/**
		 * 是否允许鼠标滑入滑出,Label组件不接受鼠标事件，此处永远返回false
		 */
		public function set mouseHoverEnabled(value:Boolean):void
		{
			//Label不接受鼠标事件
		}
		public function get mouseHoverEnabled():Boolean
		{
			return false;
		}
		
		/** 
		 * 检测是否碰撞 
		 * @param x 节点x坐标 
		 * @param y 节点y坐标 
		 * @param alphaThreshold 透明度检测，小于这个透明值的将被忽略 
		 */
		public function hitTest(x:Number,y:Number,alphaThreshold:uint = 0x00):Boolean
		{
			if(this.rect == null) return false;
			if(this.bitmapData == null) return false;
			
			var $rect:Rectangle = this.rect.clone();
			$rect.x = 0;
			$rect.y = 0;
			
			if($rect.contains(x,y))
			{
				var $point:Point = new Point(this.rect.x+x,this.rect.y+y);
				var $color:uint = this.bitmapData.getPixel32($point.x,$point.y);
				
				if(($color >> 24) < alphaThreshold)
				{
					return true;
				}
			}
			return false;
		}
		
		darwin function set textField(value:TextField):void
		{
			_textFieldContainer.removeChild(_textField);
			_textField = value;
			_textFieldContainer.addChild(_textField);
			htmlText = _textField.htmlText;
			refresh();
		}
		
		public function exeRenderNotify():void
		{
			
		}
		
		private var _renderNotify:Boolean = false;
		
		public function set renderNotify(value:Boolean):void
		{
			_renderNotify = value;
		}
		
		public function get renderNotify():Boolean
		{
			return _renderNotify;
		}
		
		/**
		 * 将 format 参数指定的文本格式应用于文本字段中的指定文本。
		 */		
		public function setTextFormat(format:TextFormat, beginIndex:int=-1, endIndex:int=-1):void
		{
			_textField.setTextFormat(format, beginIndex, endIndex);
			_textField.defaultTextFormat = format;
			changed = true;
		}
		
	}
}